package com.slz3.business.module.configManagement.conFeedback.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.configManagement.conVersion.domain.OperatingSystemEnum;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.stream.Collectors;

public class ConFeedbackFactory {

    /**
     * 初始化基础属性值
     */
    public static ConFeedback initBaseFields(ConFeedback conFeedback) {
        if (conFeedback.getUuid() == null) {
            conFeedback.setUuid(UUIDUtils.generateUUID());
            conFeedback.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(conFeedback.getSerAudit())) {
                conFeedback.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(conFeedback.getSerUsing())) {
                conFeedback.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            conFeedback.setLastModifyTime(LocalDateTime.now());
        }
        return conFeedback;
    }

    /**
     * 初始化基础属性值
     */
    public static ConFeedback initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static ConFeedback newTemStyle() {
        return new ConFeedback();
    }

    public static ConFeedbackCondition newConFeedbackCondition(MultiValueMap<String, String> params) {

        ConFeedbackCondition condition = new ConFeedbackCondition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String serAuditList = params.getFirst("serAuditList");
        String serOperatingSystemList = params.getFirst("serOperatingSystemList");

        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, ConFeedbackCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, ConFeedbackCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, ConFeedbackCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, ConFeedbackCondition.CREATE_TIME_OF_PATTERN));
        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : Arrays.stream(serAuditList.split(",")).map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));
        condition.setSerOperatingSystemList(VE.isEmpty(serOperatingSystemList) ? null : Arrays.stream(serOperatingSystemList.split(",")).map(item -> OperatingSystemEnum.valueOf(item)).collect(Collectors.toList()));
        return condition;
    }

}
